package cat.controller;

import cat.entity.Article;
import cat.service.ArticleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * 文章控制层
 * author:zhang
 */

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Resource
    ArticleService articleService;

    /**
     * 转到文章列表页
     * @param pageIndex 用于分页,表是当前是第几页
     * @param pageSize 用于分页,表示每页显示多少条数据
     */
    @RequestMapping(value = "")
    public String index(@RequestParam(required = false,defaultValue = "1") Integer pageIndex,
                        @RequestParam(required = false,defaultValue = "10") Integer pageSize, ModelMap m){

        PageInfo<Article> articlePageInfo = articleService.getPageArticleList(pageIndex,pageSize);

        m.put("pageInfo",articlePageInfo);

        m.put("pageUrlPrefix","article?pageIndex");  //把前缀传给分页的页面
        return "/Article/article-list";
    }



    @RequestMapping("/delete/{articleId}")
    public String deleteArticle(@PathVariable("articleId") Integer articleId){
        System.out.println(articleId);
        articleService.deleteArticleById(articleId);
        return "redirect:/article";
    }

}
